﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 经纪公司交易信息
    /// </summary>
    public class ServiceBrokerageDayTradeInfo : Benefit.Interface.Day.IBrokerageDayTradeInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceBrokerageDayTradeInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.BrokerageDayTradeInfo where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.BrokerageDayTradeInfo d in query)
            {
                db.BrokerageDayTradeInfo.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存经纪公司日交易数据
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBrokerDayTradeInfo(int tradeHistoryId, string pdate)
        {
            var query = db.Broker.ToList();
            Interface.ServerData.IT_TradeDetail_History th = new ServerData.ServiceT_TradeDetail_History(db);
          //  Models.ServerData.T_TradeDetail_History th = new Models.ServerData.T_TradeDetail_History();
            foreach (Models.Sys.Broker broker in query)
            {
                Models.Day.BrokerageDayTradeInfo brokerday = new Models.Day.BrokerageDayTradeInfo();
                brokerday.BillCount = th.GetBrokerDayBillCount(pdate, broker.BrokerId);
                brokerday.BrokerId = broker.Id;
                brokerday.DayCount = th.GetDayBrokerCloseProfit(pdate, broker.BrokerId) - th.GetDayBrokerCommission(pdate, broker.BrokerId);
                brokerday.Free = th.GetDayBrokerCommission(pdate, broker.BrokerId);
                brokerday.LoseCount = th.GetDayBrokerLoseCount(pdate, broker.BrokerId);
                brokerday.Profit = th.GetDayBrokerCloseProfit(pdate, broker.BrokerId);
                brokerday.SumLost = th.GetBrokerDayAllLose(pdate, broker.BrokerId);
                brokerday.SumWin = th.GetBrokerDayAllWin(pdate, broker.BrokerId);
                brokerday.TradeHistoryId = tradeHistoryId;
                brokerday.UsedMargin = th.GetDayBrokerMargin(pdate, broker.BrokerId);
                brokerday.WinCount = th.GetBrokerDayWinCount(pdate, broker.BrokerId);
                db.BrokerageDayTradeInfo.Add(brokerday);
            }
            db.SaveChanges();
        }
    }
}